<?php
Loader::loadFile('config.php', 'modules/IShopStore');

 /**
  * initialise the tour module
  *
  */
function IShopStore_init() {
  if (!DBUtil::createTable('ishopstore_categories')) {
      return false;
  }
  if (!DBUtil::createTable('ishopstore_subcategories')) {
      return false;
  }
  if (!DBUtil::createTable('ishopstore_item')) {
      return false;
  }
  if (!DBUtil::createTable('ishopstore_images')) {
      return false;
  }
  if (!DBUtil::createTable('ishopstore_order')) {
      return false;
  }
  if (!DBUtil::createTable('ishopstore_order_item')) {
      return false;
  }
  if (!DBUtil::createTable('ishopstore_shipment')) {
      return false;
  }
  if (!DBUtil::createTable('ishopstore_payment')) {
      return false;
  }
  if (!DBUtil::createTable('ishopstore_inform')) {
      return false;
  }
  if (!DBUtil::createTable('ishopstore_about')) {
      return false;
  }
  if (!DBUtil::createTable('ishopstore_contactus')) {
      return false;
  }
  CretateFilesDirectory();
  init_about();
  return true;
}

function IShopStore_delete() {
  // drop table
  DBUtil::dropTable('ishopstore_categories');
  DBUtil::dropTable('ishopstore_subcategories');
  DBUtil::dropTable('ishopstore_item');
  DBUtil::dropTable('ishopstore_images');
  DBUtil::dropTable('ishopstore_order');
  DBUtil::dropTable('ishopstore_order_item');
  DBUtil::dropTable('ishopstore_shipment');
  DBUtil::dropTable('ishopstore_payment');
  DBUtil::dropTable('ishopstore_inform');
  DBUtil::dropTable('ishopstore_about');
  DBUtil::dropTable('ishopstore_contactus');

  //RemoveDir(UPLOAD_FILES_PATH);
  RemoveDirRecurse("resource/ishopstore_upload/");

  return true;
}


// so I slightly modified the contribution from
// stefano at takys dot it (28-Dec-2005 11:57)
// A short but powerfull recursive function
// that works also if the dirs contain hidden files
//
// $dir = the target directory
// $DeleteMe = if true delete also $dir, if false leave it alone

function CretateFilesDirectory(){

      //Step 1: Make [resource] directory
      if (!is_dir("resource/")) {
        mkdir("resource/", 0777);
      }

      //Step 2: Make [ishopstore_upload] directory
      if (!is_dir("resource/ishopstore_upload/")) {
        mkdir("resource/ishopstore_upload/", 0777);
      }
      copy('modules/IShopStore/.htaccess', "resource/ishopstore_upload/.htaccess");
      copy('modules/IShopStore/index.html', "resource/ishopstore_upload/.html");

  
      //Step 3: Make [images] directory
      if (!is_dir("resource/ishopstore_upload/images/")) {
        mkdir("resource/ishopstore_upload/images/", 0777);
      }
      copy('modules/IShopStore/.htaccess','resource/ishopstore_upload/images/.htaccess');
      copy('modules/IShopStore/index.html','resource/ishopstore_upload/images/.html');
      

  }      

/*
function RemoveDir($dir, $DeleteMe = false) {
  if(!$dh = @opendir($dir)) return;
  while (false !== ($obj = readdir($dh))) {
      if($obj=='.' || $obj=='..') continue;
      if (!@unlink($dir.'/'.$obj)) RemoveDir($dir.'/'.$obj, true);
  }
  if ($DeleteMe){
      closedir($dh);
      @rmdir($dir);
  }
}
*/

function init_about(){
    $dataArray= array("0"=>array("id"=>"1",
                          "how_to"=>"",
                          "about_us"=>"",
                          "concact_us"=>""
                          )
                    );
    if ($dataArray){
      DBUtil::insertObjectArray($dataArray, 'ishopstore_about', true);
      unset($dataArray);
    }
}



function RemoveDirRecurse($path)
{
    $path= rtrim($path, '/').'/';
    $handle = opendir($path);
    for (;false !== ($file = readdir($handle));)
        if($file != "." and $file != ".." )
        {
            $fullpath= $path.$file;
            if( is_dir($fullpath) )
            {
                RemoveDirRecurse($fullpath);
                rmdir($fullpath);
            }
            else
              unlink($fullpath);
        }
    closedir($handle);
}

function RemoveFile($file)
{
  if (is_file($file)){
    @unlink($file);
  }
}


?>